﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace YSLPages
{
    using HELOAMIMAGE = IntPtr;
    using HELOAMIMAGELIST = IntPtr;
    using HELOAMFTP = IntPtr;
    using HELOAMHTTP = IntPtr;
    using HELOAMDEVICE = IntPtr;
    using HELOAMVIDEO = IntPtr;
    using HELOAMVIEW = IntPtr;
    using HELOAMMEMORY = IntPtr;
    using HELOAMBASE64 = IntPtr;
    using HELOAMTHUMBNAIL = IntPtr;
    using HELOAMRECT = IntPtr;
    using HELOAMFONT = IntPtr;
    using HELOAMVIDEOCAP = IntPtr;
    using LPVOID = IntPtr;
    using LONG = Int32;
    using BOOL = Int32;
    using HWND = IntPtr;
    using COLORREF = UInt32;

    // callback
    public delegate void ELOAM_DEVCHANGECALLBACK(LONG type, LONG idx, LONG dbt, LPVOID userData);
    public delegate void ELOAM_ARRIVALCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData);
    public delegate void ELOAM_TOUCHCALLBACK(HELOAMVIDEO video, LPVOID userData);
    public delegate void ELOAM_MOVEDETECCALLBACK(HELOAMVIDEO video, LONG id, LPVOID userData);
    public delegate void ELOAM_ATTACHCALLBAK(HELOAMVIDEO video, LONG videoId, HELOAMVIEW view, LONG viewId, LPVOID userData);
    public delegate void ELOAM_IDCARDCHANGECALLBACK(LONG dbt, LPVOID userData);

    public class EloamDll
    {

        private const string dllPath = "D:/Devices/HightCaptureLT/eloamDll.dll";

        public static string GetDllPath()
        {
            Directory.SetCurrentDirectory("D:/Devices/HightCaptureLT");
            return Path.GetFullPath(dllPath);
        }
        //         global
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DestroyString",
            CharSet = CharSet.Unicode)]
        public static extern BOOL EloamGlobal_DestroyString(IntPtr str);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreateImage")]
        public static extern HELOAMIMAGE EloamGlobal_CreateImage(LONG width, LONG height, LONG channels);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreateRect")]
        public static extern HELOAMRECT EloamGlobal_CreateRect(LONG x, LONG y, LONG width, LONG height);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreateView")]
        public static extern HELOAMVIEW EloamGlobal_CreateView(HWND hWnd, HELOAMRECT rect, LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreateThumbnail")]
        public static extern HELOAMTHUMBNAIL EloamGlobal_CreateThumbnail(HWND hWnd, HELOAMRECT rect, LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_InitDevs")]
        public static extern BOOL EloamGlobal_InitDevs(ELOAM_DEVCHANGECALLBACK fun, LPVOID userData);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DeinitDevs")]
        public static extern BOOL EloamGlobal_DeinitDevs();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreateDevice")]
        public static extern HELOAMDEVICE EloamGlobal_CreateDevice(LONG type, LONG idx);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_InitBarcode")]
        public static extern BOOL EloamGlobal_InitBarcode();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DeinitBarcode")]
        public static extern BOOL EloamGlobal_DeinitBarcode();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DiscernBarcode")]
        public static extern BOOL EloamGlobal_DiscernBarcode(HELOAMIMAGE img);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_GetBarcodeCount", CallingConvention = CallingConvention.Cdecl)]
        public static extern LONG EloamGlobal_GetBarcodeCount();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_GetBarcodeData",
            CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        public static extern string EloamGlobal_GetBarcodeData(LONG idx);

        [DllImport(dllPath, EntryPoint = "EloamGlobal_VideoCapInit", CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_VideoCapInit();

        [DllImport(dllPath, EntryPoint = "EloamGlobal_VideoCapStop", CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_VideoCapStop(HELOAMVIDEOCAP cap);

        [DllImport(dllPath, EntryPoint = "EloamGlobal_DestroyVideoCap", CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_DestroyVideoCap(HELOAMVIDEOCAP cap);

        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreatVideoCap", CallingConvention = CallingConvention.Cdecl)]
        public static extern HELOAMVIDEOCAP EloamGlobal_CreatVideoCap();

        [DllImport(dllPath, EntryPoint = "EloamGlobal_VideoCapGetAudioDevNum", CallingConvention = CallingConvention.Cdecl)]
        public static extern LONG EloamGlobal_VideoCapGetAudioDevNum();

        [DllImport(dllPath, EntryPoint = "EloamGlobal_VideoCapPreCap",
            CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_VideoCapPreCap(HELOAMVIDEOCAP m_cap, string fileName, LONG nSelectMic, LONG FrameRate, LONG compressMode, LONG width, LONG Height, BOOL bCapVideo);

        [DllImport(dllPath, EntryPoint = "EloamGlobal_VideoCapStart", CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_VideoCapStart(HELOAMVIDEOCAP cap);

        [DllImport(dllPath, EntryPoint = "EloamGlobal_VideoCapAddVideoSrc",
            CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_VideoCapAddVideoSrc(HELOAMVIDEOCAP cap, HELOAMVIDEO mVideo);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_InitIdCard",
                CallingConvention = CallingConvention.Cdecl)]
        //[DllImport(dllPath, EntryPoint = "EloamGlobal_InitIdCard")]
        public static extern BOOL EloamGlobal_InitIdCard(ELOAM_IDCARDCHANGECALLBACK fun, LPVOID userData);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DeinitIdCard",
               CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_DeinitIdCard();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_ReadIdCard",
                   CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_ReadIdCard();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DiscernIdCard",
                       CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_DiscernIdCard(ELOAM_IDCARDCHANGECALLBACK fun, LPVOID userData);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_GetIdCardImage")]
        public static extern HELOAMIMAGE EloamGlobal_GetIdCardImage(LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_GetIdCardData",
                    CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        //[DllImport(dllPath, EntryPoint = "EloamGlobal_GetIdCardData",
        //CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
        public static extern string EloamGlobal_GetIdCardData(LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_StopIdCardDiscern",
                                   CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_StopIdCardDiscern();


        [DllImport(dllPath, EntryPoint = "EloamGlobal_InitFaceDetect",
                                       CallingConvention = CallingConvention.Cdecl)]
        public static extern BOOL EloamGlobal_InitFaceDetect();
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DiscernFaceDetect")]
        public static extern LONG EloamGlobal_DiscernFaceDetect(HELOAMIMAGE img1, HELOAMIMAGE img2);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_GetFaceRect")]
        public static extern HELOAMRECT EloamGlobal_GetFaceRect(HELOAMIMAGE img);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_EnableFaceRectCrop")]
        public static extern HELOAMRECT EloamGlobal_EnableFaceRectCrop(HELOAMVIDEO video, LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DisableFaceRectCrop")]
        public static extern HELOAMRECT EloamGlobal_DisableFaceRectCrop(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamGlobal_DeinitFaceDetect")]
        public static extern HELOAMRECT EloamGlobal_DeinitFaceDetect();


        //          // image
        [DllImport(dllPath, EntryPoint = "EloamImage_Release")]
        public static extern LONG EloamImage_Release(HELOAMIMAGE img);
        [DllImport(dllPath, EntryPoint = "EloamImage_Save",
            CharSet = CharSet.Unicode)]
        public static extern BOOL EloamImage_Save(HELOAMIMAGE img, string fileName, LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamImage_GetWidth")]
        public static extern LONG EloamImage_GetWidth(HELOAMIMAGE img);
        [DllImport(dllPath, EntryPoint = "EloamImage_GetHeight")]
        public static extern LONG EloamImage_GetHeight(HELOAMIMAGE img);
        [DllImport(dllPath, EntryPoint = "EloamImage_Blend")]
        public static extern BOOL EloamImage_Blend(HELOAMIMAGE imgDest, HELOAMRECT rectDest, HELOAMIMAGE imgSrc, HELOAMRECT rectSrc, LONG weight, LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamMemory_Release")]
        public static extern LONG EloamMemory_Release(HELOAMMEMORY mem);

        //EloamImage_DelImageBackColor
        [DllImport(dllPath, EntryPoint = "EloamImage_DelImageBackColor")]
        public static extern BOOL EloamImage_DelImageBackColor(HELOAMIMAGE image);

        //          // device
        [DllImport(dllPath, EntryPoint = "EloamDevice_Release")]
        public static extern LONG EloamDevice_Release(HELOAMDEVICE dev);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetFriendlyName")]
        public static extern IntPtr EloamDevice_GetFriendlyName(HELOAMDEVICE dev);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetSubtype")]
        public static extern LONG EloamDevice_GetSubtype(HELOAMDEVICE dev);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetResolutionCount")]
        public static extern LONG EloamDevice_GetResolutionCount(HELOAMDEVICE dev);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetResolutionWidth")]
        public static extern LONG EloamDevice_GetResolutionWidth(HELOAMDEVICE dev, LONG idx);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetResolutionHeight")]
        public static extern LONG EloamDevice_GetResolutionHeight(HELOAMDEVICE dev, LONG idx);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetResolutionCountEx")]
        public static extern LONG EloamDevice_GetResolutionCountEx(HELOAMDEVICE dev, LONG subtype);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetResolutionWidthEx")]
        public static extern LONG EloamDevice_GetResolutionWidthEx(HELOAMDEVICE dev, LONG subtype, LONG idx);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetResolutionHeightEx")]
        public static extern LONG EloamDevice_GetResolutionHeightEx(HELOAMDEVICE dev, LONG subtype, LONG idx);
        [DllImport(dllPath, EntryPoint = "EloamDevice_ShowProperty")]
        public static extern BOOL EloamDevice_ShowProperty(HELOAMDEVICE dev, HWND hWnd);
        [DllImport(dllPath, EntryPoint = "EloamDevice_CreateVideo")]
        public static extern HELOAMVIDEO EloamDevice_CreateVideo(HELOAMDEVICE dev, LONG resolution, LONG subtype, ELOAM_ARRIVALCALLBACK funArrival, LPVOID userArrival, ELOAM_TOUCHCALLBACK funTouch, LPVOID userTouch, int resolutionStillCap, int subtypeStillCap);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetEloamType")]
        public static extern LONG EloamDevice_GetEloamType(HELOAMDEVICE dev);
        [DllImport(dllPath, EntryPoint = "EloamDevice_GetIndex")]
        public static extern LONG EloamDevice_GetIndex(HELOAMDEVICE dev);
        //         
        //          // video
        [DllImport(dllPath, EntryPoint = "EloamVideo_Release")]
        public static extern LONG EloamVideo_Release(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_GetDevice")]
        public static extern HELOAMDEVICE EloamVideo_GetDevice(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_CreateImage")]
        public static extern HELOAMIMAGE EloamVideo_CreateImage(HELOAMVIDEO video, LONG flag, HELOAMVIEW view);
        [DllImport(dllPath, EntryPoint = "EloamVideo_RotateLeft")]
        public static extern BOOL EloamVideo_RotateLeft(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_RotateRight")]
        public static extern BOOL EloamVideo_RotateRight(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_Flip")]
        public static extern BOOL EloamVideo_Flip(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_Mirror")]
        public static extern BOOL EloamVideo_Mirror(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_EnableDelBkColor")]
        public static extern BOOL EloamVideo_EnableDelBkColor(HELOAMVIDEO video, LONG flag);
        [DllImport(dllPath, EntryPoint = "EloamVideo_DisableDelBkColor")]
        public static extern BOOL EloamVideo_DisableDelBkColor(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_EnableDeskew")]
        public static extern BOOL EloamVideo_EnableDeskew(HELOAMVIDEO video, LONG flag, int nOffset);
        [DllImport(dllPath, EntryPoint = "EloamVideo_DisableDeskew")]
        public static extern BOOL EloamVideo_DisableDeskew(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_EnableMoveDetec")]
        public static extern BOOL EloamVideo_EnableMoveDetec(HELOAMVIDEO video, LONG flag, ELOAM_MOVEDETECCALLBACK fun, LPVOID userData);
        [DllImport(dllPath, EntryPoint = "EloamVideo_DisableMoveDetec")]
        public static extern BOOL EloamVideo_DisableMoveDetec(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_GetWidth")]
        public static extern LONG EloamVideo_GetWidth(HELOAMVIDEO video);
        [DllImport(dllPath, EntryPoint = "EloamVideo_GetHeight")]
        public static extern LONG EloamVideo_GetHeight(HELOAMVIDEO video);


        //         
        //          // view
        [DllImport(dllPath, EntryPoint = "EloamView_Release")]
        public static extern LONG EloamView_Release(HELOAMVIEW view);
        [DllImport(dllPath, EntryPoint = "EloamView_SelectVideo")]
        public static extern BOOL EloamView_SelectVideo(HELOAMVIEW view, HELOAMVIDEO video, ELOAM_ATTACHCALLBAK fun, LPVOID userData);
        [DllImport(dllPath, EntryPoint = "EloamView_SetText",
            CharSet = CharSet.Unicode)]
        public static extern BOOL EloamView_SetText(HELOAMVIEW view, string text, COLORREF clr);
        [DllImport(dllPath, EntryPoint = "EloamView_PlayCaptureEffect")]
        public static extern BOOL EloamView_PlayCaptureEffect(HELOAMVIEW view);
        //         
        //          // rect
        [DllImport(dllPath, EntryPoint = "EloamRect_Release")]
        public static extern LONG EloamRect_Release(HELOAMRECT rect);
        //         
        //          // thumbnail
        [DllImport(dllPath, EntryPoint = "EloamThumbnail_Release")]
        public static extern LONG EloamThumbnail_Release(HELOAMTHUMBNAIL thumb);
        [DllImport(dllPath, EntryPoint = "EloamThumbnail_Add",
            CharSet = CharSet.Unicode)]
        public static extern BOOL EloamThumbnail_Add(HELOAMTHUMBNAIL thumb, string imagePath);

        /// <summary>
        /// 添加图片到pdf列表
        /// </summary>
        /// <param name="fileName">表示需要添加图片的文件全路径名</param>
        /// <returns></returns>
        [DllImport(dllPath, EntryPoint = "EloamGlobal_AddFile",
            CharSet = CharSet.Unicode)]
        public static extern BOOL EloamGlobal_AddFile(string fileName);        

        /// <summary>
        /// 创建pdf
        /// </summary>
        /// <param name="fileName">表示创建pdf的文件全路径名</param>
        /// <returns></returns>
        [DllImport(dllPath, EntryPoint = "EloamGlobal_CreatePDF",
           CharSet = CharSet.Unicode)]
        public static extern BOOL EloamGlobal_CreatePDF(string fileName);

        /// <summary>
        /// 返回pdf列表图片的数量
        /// </summary>
        /// <returns></returns>
        [DllImport(dllPath, EntryPoint = "EloamGlobal_GetFileCount",
            CharSet = CharSet.Unicode)]
        public static extern LONG EloamGlobal_GetFileCount();

        /// <summary>
        /// 清空pdf图片列表
        /// </summary>
        /// <returns></returns>
        [DllImport(dllPath, EntryPoint = "EloamGlobal_ClearFile",
            CharSet = CharSet.Unicode)]
        public static extern LONG EloamGlobal_ClearFile();
    }
}
